
#import "RefreshedTableView.h"


@interface RefreshedTableView()<SRRefreshDelegate,UIScrollViewDelegate>

@end
#define kScreenWidth [[UIScreen mainScreen]bounds].size.width
@implementation RefreshedTableView
{
    SRRefreshView *_smileView;
    UIActivityIndicatorView *activeIndicator;
}

-(id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    self=[super initWithFrame:frame style:style];
    if(self){
        _smileView=[[SRRefreshView alloc]init];
        _smileView.delegate=self;
        _smileView.upInset = 0.0;
        _smileView.slimeMissWhenGoingBack=YES;
        _smileView.slime.bodyColor=[UIColor blackColor];
        _smileView.slime.skinColor=[UIColor whiteColor];
        _smileView.slime.lineWith=1;
        _smileView.slime.shadowBlur=4;
        _smileView.slime.shadowColor=[UIColor blackColor];
        [self addSubview:_smileView];
    }
    return  self;
}

-(void)addFooter{
    UIView *footer = self.tableFooterView;
    if (footer == nil) {
        UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
        activeIndicator=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        activeIndicator.center=view.center;
        activeIndicator.color=[UIColor lightGrayColor];
        [activeIndicator stopAnimating];
        [view addSubview:activeIndicator];
        self.tableFooterView=view;
    }else{
        UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, footer.frame.origin.y, kScreenWidth, 20)];
        activeIndicator=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        activeIndicator.center=CGPointMake(kScreenWidth / 2, 10);
        activeIndicator.color=[UIColor lightGrayColor];
        [activeIndicator stopAnimating];
        [view addSubview:activeIndicator];
        [self.tableFooterView addSubview:view];
    }
}

-(void)setStartAnimation:(BOOL)startAnimation{
    _startAnimation = startAnimation;
    NSThread *thread = [NSThread currentThread];
    if ([thread isMainThread] ) {
        if(startAnimation){
            [activeIndicator startAnimating];
        }else{
            [activeIndicator stopAnimating];
        }
    }else{
        dispatch_async(dispatch_get_main_queue(), ^{
            if(startAnimation){
                [activeIndicator startAnimating];
            }else{
                [activeIndicator stopAnimating];
            }
        });
    }
}

-(void)slimeRefreshStartRefresh:(SRRefreshView *)refreshView{
    NSTimer *timer = [NSTimer timerWithTimeInterval:10 target:self selector:@selector(end:) userInfo:nil repeats:NO];
    [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
    [self.refreshDelegate beginRefresh];
}

-(void)end:(NSTimer *)tim{
    [tim invalidate];
    NSThread *thread = [NSThread currentThread];
    if([thread isMainThread]){
        [_smileView endRefresh];
    }else{
        dispatch_async(dispatch_get_main_queue(), ^{
            [_smileView endRefresh];
        });
    }
}


@end
